<?php
require 'common.php';
require 'mysql.php';
 


//db //$dbhost, $dbport, $dbuser, $dbpw, $dbname

$game_db = new DBPDO($db_config);


$redis_conf = array('ip'=>'127.0.0.1','port'=>6999,'auth'=>'','db'=>1);

$redis = new Redis();
$redis->connect($redis_conf['ip'], $redis_conf['port']);
if($redis_conf['auth']){ 
	$redis->auth($redis_conf['auth']);
}
$redis->select($redis_conf['db']);

function get_mid_level($uid){
	global $redis;
	global $game_db;

	$higher = $redis->hget('higher',$uid);
	if($higher == false){ 
		$sql = 'select * from higher where uid='.$uid;
		$rs = $game_db->select($sql);   
		if(!empty($rs[0]) and $rs[0]['higher'] ){ 
			$higher = $rs[0]['higher'];
			$redis->hset('higher',$uid,$higher);
		}
	}
	$rs_mid = array();
	if($higher){
		$higher = json_decode($higher,true);
	// var_dump($higher);
	// echo '===============================================';
		if(!empty($higher['level1']) and $higher['level1']['uid'] > 0){
			$rs_mid[1] = $higher['level1']['uid'];
		}
		if(!empty($higher['level2']) and $higher['level2']['uid'] > 0){
			$rs_mid[2] = $higher['level2']['uid'];
		}
		if(!empty($higher['level3']) and $higher['level3']['uid'] > 0){
			$rs_mid[3] = $higher['level3']['uid'];
		}
		if(!empty($higher['level4']) and $higher['level4']['uid'] > 0){
			$rs_mid[4] = $higher['level4']['uid'];
		}
	}
	// var_dump($rs_mid);
	return $rs_mid; 

}

 
 
for ($kk=0;$kk<999;$kk++){  
	for ($i=0;$i<10;$i++){  
		$table_name = 'bean_log_'.$i; 
		try{
			$game_db->beginTransaction();//开启事务处理
			$sql = 'select * from '.$table_name.' where flag = 0 limit 0,1';
			$rs = $game_db->select($sql);  
			// print_r($rs);
			if(!empty($rs[0]) and $rs[0]['id'] > 0){
				$id = $rs[0]['id'];

				$sql = 'UPDATE '.$table_name.' set flag = 1 where id='.$id;
				// echo $sql."\n";
				$rs_update = $game_db->update($sql);   
				if($rs_update == 1){
					$bean_all = 0;
					$bean = $rs[0]['bean_time'] * $bean_qc_lv;
					$text_str = $rs[0]['text'];
					$text = json_decode($text_str,true);

					$bean_my = $bean * rand(80,120) / 100;
					$bean_all += $bean_my;
					$level = get_mid_level($rs[0]['uid']);
					$count = count($level);
					foreach ($level as $key => $value) {
						$uid = $value;
						$table_qc = 'user_qc_log_'.($uid % 10); 
						$bean_my = $bean_my / $count;
						$state = $key;
						$sql = 'INSERT INTO '.$table_qc.' (`uid`, `qc`, `text`, `type`, `from_id`, `create_time`, `state`) VALUES ('.$uid.','.$bean_my.',\''.$text_str.'\',"'.$rs[0]['type'].'","'.$rs[0]['id'].'","'.$rs[0]['date'].'",'.$state.')';
						// echo $sql."\n";
						$rs_update = $game_db->update($sql);
					}


					$uid = $rs[0]['uid'];
					$table_qc = 'user_qc_log_'.($uid % 10); 
					$bean_my = $bean * rand(80,110) / 100;
					$bean_all += $bean_my;
					$state = 11; //自己
					$sql = 'INSERT INTO '.$table_qc.' (`uid`, `qc`, `text`, `type`, `from_id`, `create_time`, `state`) VALUES ('.$uid.','.$bean_my.',\''.$text_str.'\',"'.$rs[0]['type'].'","'.$rs[0]['id'].'","'.$rs[0]['date'].'",'.$state.')';
					// echo $sql."\n";
					$rs_update = $game_db->update($sql);
	 
	 				if(isset($text['create_uid']) and $text['create_uid'] > 0){
						$uid = $text['create_uid'];
						$table_qc = 'user_qc_log_'.($uid % 10); 
						$bean_my = $bean * rand(80,110) / 100;
						$bean_all += $bean_my;
						$state = 12; //房主
						if( $text['club_id'] > 0 ){
							$state = 13; //俱乐部
						}
						$sql = 'INSERT INTO '.$table_qc.' (`uid`, `qc`, `text`, `type`, `from_id`, `create_time`, `state`) VALUES ('.$uid.','.$bean_my.',\''.$text_str.'\',"'.$rs[0]['type'].'","'.$rs[0]['id'].'","'.$rs[0]['date'].'",'.$state.')';
						// echo $sql."\n";
						$rs_update = $game_db->update($sql); 
						if($rs_update != 11){
							throw new PDOException();
						} 
	 				}


					$bean_my = $rs[0]['bean_time'] - $bean_all;
					$table_qc = 'user_qc_log'; 
					$state = 0; //平台
					$sql = 'INSERT INTO '.$table_qc.' (`uid`, `qc`, `text`, `type`, `from_id`, `create_time`, `state`) VALUES (0,'.$bean_my.',\''.$text_str.'\',"'.$rs[0]['type'].'","'.$rs[0]['id'].'","'.$rs[0]['date'].'",'.$state.')';
					// echo $sql."\n";
					$rs_update = $game_db->update($sql);
	 
					
				}else{
					throw new PDOException();
				} 
			} 
			$game_db->commit();//交易成功就提交
		}catch(PDOException $e){ 
			$game_db->rollback();
		} 
	} 
} 

 








